<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

</head>

<body>
    <script>
        var tree = {
            "id": 0,
            "name": "root",
            "left": {
                "id": 1,
                "name": "Simon",
                "left": {
                    "id": 3,
                    "name": "Carl",
                    "left": {
                        "id": 7,
                        "name": "Lee",
                        "left": {
                            "id": 11,
                            "name": "Fate"
                        }
                    },
                    "right": {
                        "id": 8,
                        "name": "Annie",
                        "left": {
                            "id": 12,
                            "name": "Saber"
                        }
                    }
                },
                "right": {
                    "id": 4,
                    "name": "Tony",
                    "left": {
                        "id": 9,
                        "name": "Candy"
                    }
                }
            },
            "right": {
                "id": 2,
                "name": "right",
                "left": {
                    "id": 5,
                    "name": "Carl",
                },
                "right": {
                    "id": 6,
                    "name": "Carl",
                    "right": {
                        "id": 10,
                        "name": "Kai"
                    }
                }
            }
        }

        // 假设id和name均不会重复，根据输入name找到对应的id
        function findIdByName(name) {
            var id;

            function findName(obj) {
                if (obj != null) {
                    if (name === obj.name) {
                        id = obj.id;
                        console.log(id);
                    }
                    findName(obj.left);
                    findName(obj.right);
                }
            }
            findName(tree);
        }
        findIdByName('Candy')

        // 假设id和name均不会重复，根据输入id找到对应的name
        function findNameById(id) {
            var name;

            function findId(obj) {
                if (obj != null) {
                    if (id === obj.id) {
                        name = obj.name;
                        console.log(name);
                    }
                    findId(obj.left);
                    findId(obj.right);
                }

            }
            findId(tree);
        }
        findNameById(9);
        // 把这个对象中所有的名字以“前序遍历”的方式全部输出到console中
        function getListWithDLR() {
            function preTrans(obj) {
                if (obj == null) {
                    return;
                }
                console.log(obj.name, obj.id);
                preTrans(obj.left);
                preTrans(obj.right);
            }
            preTrans(tree);
        }
        getListWithDLR();
        // 把这个对象中所有的名字以“中序遍历”的方式全部输出到console中
        function getListWithLDR() {
            function midTrans(obj) {
                if (obj == null) {
                    return;
                }
                midTrans(obj.left);
                console.log(obj.name, obj.id);
                midTrans(obj.right);
            }
            midTrans(tree);
        }
        getListWithLDR();
        // 把这个对象中所有的名字以“后序遍历”的方式全部输出到console中
        function getListWithLRD() {
            function postTrans(obj) {
                if (obj == null) {
                    return;
                }
                postTrans(obj.left);
                postTrans(obj.right);
                console.log(obj.name, obj.id);
            }
            postTrans(tree);
        }
        getListWithLRD();
    </script>
</body>

</html>